#include <iostream>

using namespace std;

class Context;

class State {
public:
    virtual void Handle(Context* c) = 0;
};

class Context {
private:
    State* m_state;
public:
    Context(State* p):
        m_state(p) {
    }
    void Request() {
        if (m_state != NULL) {
            m_state->Handle(this);
        }
    }
    void changeState(State* s) {
        if (m_state != NULL) {
            delete m_state;
            m_state = NULL;
        }
        m_state = s;
    }
};


class ConcretStateA: public State {
public:
    void Handle(Context* c);
};


class ConcretStateB: public State {
public:
    void Handle(Context* c);
};

void ConcretStateA::Handle(Context* c) {
    cout << "Handle by ConcretStateA" << endl;
    if (c != NULL) {
        c->changeState(new ConcretStateB());
    }
}


void ConcretStateB::Handle(Context* c) {
    cout << "Handle by ConcretStateB" << endl;
    if (c != NULL) {
        c->changeState(new ConcretStateA());
    }
}

int main(int argc, char** argv) {
    Context* c = new Context(new ConcretStateA());
    c->Request();
    c->Request();
    c->Request();
    c->Request();
    c->Request();
    c->Request();
    
}
